import com.jeeplus.common.json.ReturnJson;
import com.jeeplus.core.mapper.JsonMapper;
import com.jeeplus.modules.sys.entity.TreeModel;

import java.nio.file.FileSystem;
import java.util.*;

/**
 * @author Administrator
 * @title: sss
 * @projectName JeePlus
 * @description: TODO
 * @date 2019/10/2712:35
 */

public class sss {
    public static void main(String[] args) {

        List<TreeModel> treeList = new ArrayList<>();
        TreeModel node = new TreeModel();
        node.id = "7BCDCAA4-2C65-444A-9D04-57F990585C92";
        node.text = "系统管理";
        node.value = "SysManage";
        node.showcheck = false;
        node.checkstate = 0;
        node.isexpand = true;
        node.parentId = "0";
        treeList.add(node);
        TreeModel node1 = new TreeModel();
        node1.id = "FBF3CB04-1538-48B8-94EB-C4BDC6DB3AF5";
        node1.text = "数据库版本";
        node1.value = "DbVersion";
        node1.showcheck = false;
        node1.checkstate = 0;
        node1.isexpand = true;
        node1.parentId = "7BCDCAA4-2C65-444A-9D04-57F990585C92";
        treeList.add(node1);
        System.out.println(JsonMapper.toJsonString(ToTree(treeList, "")));
    }

    public static List<TreeModel> ToTree(List<TreeModel> list, String parentId) {
        Map<String, List<TreeModel>> childrenMap = new HashMap<>();
        Map<String, TreeModel> parentMap = new HashMap<>();
        List<TreeModel> res = new ArrayList<>();
        for (TreeModel node : list) {
            node.hasChildren = false;
            node.complete = true;
            if (!childrenMap.containsKey(node.parentId)) {
                childrenMap.put(node.parentId, new ArrayList<TreeModel>());
            } else if (parentMap.containsKey(node.parentId)) {
                parentMap.get(node.parentId).hasChildren = true;
            }
            childrenMap.get(node.parentId).add(node);
            // 注册父节点映射节点表
            parentMap.put(node.id, node);
            if (!childrenMap.containsKey(node.id)) {
                childrenMap.put(node.id, new ArrayList<TreeModel>());
            } else {
                node.hasChildren = true;
            }
            node.ChildNodes = childrenMap.get(node.id);
        }
        if (parentId.isEmpty()) {
            System.out.println(JsonMapper.toJsonString(childrenMap));
            System.out.println(JsonMapper.toJsonString(parentMap));
            for (List<TreeModel> item : childrenMap.values()) {
                for (TreeModel t:item){
                    if(!parentMap.containsKey(t.parentId)){
                        System.out.println(t.parentId);
                    }
                }
            }
            // 获取祖先数据列表
            for (List<TreeModel> item : childrenMap.values()) {

                if (!parentMap.containsValue(item.iterator()) && item.size() > 0) {
                    for (TreeModel t:item){
                        if(!parentMap.containsKey(t.parentId)){
                            res.add(t);
                        }
                    }

                }
            }
        } else {
            if (childrenMap.containsKey(parentId)) {
                return childrenMap.get(parentId);
            } else {
                return new ArrayList<TreeModel>();
            }
        }
        return res;
    }
}
